home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / net / netamsrc.arc / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-09  |  3.1 KB  |  140 lines

  1. /* opendir/readdir/closedir/etc - portable directory reader
  2.  * :ts=8 ma=1 bk=0
  3.  *
  4.  * DIR *opendir(name) char *name;
  5.  *    prepare to read directory "name".
  6.  *    pass the returned value to other members of this function suite.
  7.  *    NULL return indicates error.
  8.  *
  9.  * struct FileInfoBlock *readdir(dirp) DIR *dirp;
  10.  *    read the next entry in the directory.
  11.  *    NULL return indicates error or end of directory;
  12.  *    use IoErr() to distinguish.
  13.  *
  14.  * long telldir(dirp) DIR *dirp;
  15.  *    get a "place marker" for the current point in the directory.
  16.  *
  17.  * void seekdir(dirp, key) DIR *dirp; long key;
  18.  *    return to a previously noted point in the directory.
  19.  *
  20.  * void rewinddir(dirp) DIR *dirp;
  21.  *    return to the beginning of the directory.
  22.  *
  23.  * void closedir(dirp) DIR *dirp;
  24.  *    finish operations on directory.
  25.  *
  26.  * FREELY DISTRIBUTABLE
  27.  * j w hamilton, 7 nov 86
  28.  */
  29. #include <exec/types.h>
  30. #include "dir.h"
  31. #include <functions.h>
  32.  
  33. DIR *
  34. opendir(name)
  35.     char *name;
  36. {
  37.     register DIR *dirp;
  38.  
  39.     /* allocate the DIR struct
  40.      */
  41.     if (dirp = (DIR *) AllocMem((long) sizeof(DIR), 0L)) {
  42.         /* allocate DIR's FileInfoBlock struct
  43.          */
  44.         if (dirp->dd_fib = (struct FileInfoBlock *) AllocMem((long) sizeof(struct FileInfoBlock), 0L)) {
  45.             /* lock the desired directory
  46.              * if "name" is ".", open current directory
  47.              */
  48.             if (name[0] != '.' || name[1] != '\0')
  49.                 dirp->dd_lock = Lock(name, ACCESS_READ);
  50.             else {
  51.                 /* obtain a lock on the current directory
  52.                  * by chdir'ing to ":"; chdir back; and
  53.                  * duplicate the current directory lock
  54.                  */
  55.                 dirp->dd_lock = CurrentDir(0L);
  56.                 CurrentDir(dirp->dd_lock);
  57.                 dirp->dd_lock = DupLock(dirp->dd_lock);
  58.             }
  59.             if (dirp->dd_lock) {
  60.                 /* if we managed to Lock the directory,
  61.                  * there should be no prob Examine'ing it
  62.                  */
  63.                 if (Examine(dirp->dd_lock, dirp->dd_fib)) {
  64.                     /* make sure it's a directory
  65.                      */
  66.                     if (dirp->dd_fib->fib_DirEntryType > 0L)
  67.                         return(dirp);
  68.                 }
  69.                 /* without errors, we never reach here
  70.                  */
  71.                 UnLock(dirp->dd_lock);
  72.             }
  73.             FreeMem(dirp->dd_fib, (long) sizeof(struct FileInfoBlock));
  74.         }
  75.         FreeMem(dirp, (long) sizeof(DIR));
  76.     }
  77.     return(NULL);
  78. }
  79.  
  80. /* examine the next entry in the directory;
  81.  * return the FIB pointer, or NULL.
  82.  * IoErr() is available for rigor
  83.  */
  84. struct FileInfoBlock *
  85. readdir(dirp)
  86.     DIR *dirp;
  87. {
  88.     if (dirp) {
  89.         if (ExNext(dirp->dd_lock, dirp->dd_fib))
  90.             return(dirp->dd_fib);
  91.     }
  92.     return(NULL);
  93. }
  94.  
  95. /* get a "magic cookie" to mark the current position in the directory.
  96.  * use this value for seekdir()
  97.  */
  98. LONG
  99. telldir(dirp)
  100.     DIR *dirp;
  101. {
  102.     if (dirp)
  103.         return(dirp->dd_fib->fib_DiskKey);
  104.     return(NULL);
  105. }
  106.  
  107. /* move to a previously noted position in the directory
  108.  */
  109. void
  110. seekdir(dirp, key)
  111.     DIR *dirp;
  112.     LONG key;
  113. {
  114.     if (dirp)
  115.         dirp->dd_fib->fib_DiskKey = key;
  116. }
  117.  
  118. /* move back to the beginning of the directory
  119.  */
  120. void
  121. rewinddir(dirp)
  122.     DIR *dirp;
  123. {
  124.     if (dirp)
  125.         Examine(dirp->dd_lock, dirp->dd_fib);
  126. }
  127.  
  128. /* "close" the directory, release lock and memory.
  129.  */
  130. void
  131. closedir(dirp)
  132.     DIR *dirp;
  133. {
  134.     if (dirp) {
  135.         UnLock(dirp->dd_lock);
  136.         FreeMem(dirp->dd_fib, (long) sizeof(struct FileInfoBlock));
  137.         FreeMem(dirp, (long) sizeof(DIR));
  138.     }
  139. }
  140.